<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>1.生命周期1</title>
</head>

<body>
    <div id="test" />
    <script type="text/javascript"
        src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/react/18.2.0/umd/react.production.min.js"></script>
    <script type="text/javascript"
        src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/react-dom/18.2.0/umd/react-dom.production.min.js"></script>
    <script type="text/javascript"
        src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/babel-standalone/6.26.0/babel.min.js"></script>

    <script type="text/babel">
        // 生命周期钩子函数
        class Life extends React.Component {
            state = { opacity: 1 }
            death = () => {
                // 卸载组件
                ReactDOM.unmountComponentAtNode(document.getElementById("test"))
            }
            // 组件挂载完毕
            componentDidMount() {
                console.info("componentDidMount..")
                this.timer = setInterval(() => {
                    let { opacity } = this.state
                    opacity -= 0.1
                    if (opacity <= 0) opacity = 1
                    this.setState({ opacity })
                }, 200);
            }

            //组件即将要卸载
            componentWillUnmount() {
                console.info("componentWillUnmount..")
                // 清除定时器
                clearInterval(this.timer)
            }

            // render调用的时机: 初始化渲染  状态更新之后
            render() {
                console.info("render..")
                return (
                    <div>
                        <h2 style={{ opacity: this.state.opacity }}>React学不会怎么办</h2>
                        <button onClick={this.death}>不活了</button>
                    </div>
                )
            }
        }
        ReactDOM.render(<Life />, document.getElementById("test"))
    </script>
</body>

</html>